#include "organizationdialog.h"
#include "ui_organizationdialog.h"

#include <QtSql>
#include <QMessageBox>

OrganizationDialog::OrganizationDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::OrganizationDialog)
{
    ui->setupUi(this);
    ui->error_label->setHidden(true);
    setOrganizationId(0);
}

OrganizationDialog::~OrganizationDialog()
{
    //delete ui;/*crash :( */
}

void OrganizationDialog::setOrganization(const int organization_id)
{
    setWindowTitle(tr("Organization (edit)"));
    QSqlTableModel *model = new QSqlTableModel(this);
    model->setTable("organizations");
    model->select();

    QModelIndexList item = model->match(model->index( 0, model->fieldIndex("id")), Qt::DisplayRole, organization_id,  1, Qt::MatchExactly);

    ui->organization_lineEdit->setText(model->data(model->index(item[0].row(), model->fieldIndex("organization"))).toString());
    ui->town_lineEdit->setText(model->data(model->index(item[0].row(), model->fieldIndex("town"))).toString());
    ui->street_lineEdit->setText(model->data(model->index(item[0].row(), model->fieldIndex("street"))).toString());
    ui->no_lineEdit->setText(model->data(model->index(item[0].row(), model->fieldIndex("no"))).toString());

    setOrganizationId(organization_id);

}

QString OrganizationDialog::getOrganization() const
{
    return ui->organization_lineEdit->text();
}

QString OrganizationDialog::getTown() const
{
    return ui->town_lineEdit->text();
}

QString OrganizationDialog::getStreet() const
{
    return ui->street_lineEdit->text();
}

QString OrganizationDialog::getNo() const
{
    return ui->no_lineEdit->text();
}

void OrganizationDialog::accept()
{
    if(ui->organization_lineEdit->text().isEmpty()){
        this->showError(tr("Empty Organization not allowed!"));
        return;
    }
    QSqlQuery *query = new QSqlQuery(QSqlDatabase::database());
    QString q = QString("SELECT COUNT(`organization`) FROM `organizations` WHERE "
                        "`organization` LIKE '%1'")
                        .arg(ui->organization_lineEdit->text());
    if(getOrganizationId()){ //edit mode
        q.append(QString(" AND `id` NOT LIKE '%1'").arg(getOrganizationId()));
    }
    if(!query->exec(q)){
        this->showError(query->lastError().text());
        QMessageBox msgBox;
        msgBox.setText(query->lastError().text());
        msgBox.exec();
    }
    if(query->first()){
        if(query->value(0).toInt() >= 1){//duplicate organization
            this->showError(tr("Duplicate Organizations not allowed!"));
            return;
        }
    }
    QDialog::accept();
}

void OrganizationDialog::showError(const QString &msg, const int secs)
{
    ui->error_label->setText(msg);
    ui->error_label->show();
    //ui->cancel_toolButton->setDisabled(true);
    QTimer::singleShot(secs*1000, this, SLOT(hideError()));
}

void OrganizationDialog::hideError()
{
    ui->error_label->hide();
    //ui->cancel_toolButton->setDisabled(false);
}
